﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

using ESF.General;
using ESF.Web.ServerControls;
using ESF.General.Entity;
using System.Data;

#endregion


namespace ESF.Web
{
	public static class DataListLoader
	{
		#region Limpieza
		
		public static void Clear(DataList pList, DataPagging pDataPagging = null)
		{
			pList.Visible = false;
			
			pList.DataSource = null;
			pList.DataBind ();
			
			pList.SelectedIndex = -1;

			if (pDataPagging != null)
			{
				pDataPagging.PageIndexNoChanges = 0;
				pDataPagging.ShowEmptyMessage ();
				pDataPagging.Visible = false;
				
				pDataPagging.ChangeToDefaultPageSize();
			}
		}
		
		#endregion
	
	
		#region Carga
		
		public static bool LoadPagedDataList<O> (DataList pDataList, DataPagging pDataPagging, BrowseResults<O> pPagedValues)
		{
			Clear(pDataList);

			pDataPagging.Visible = true; 
			
			if (DataHelper.IsEmpty (pPagedValues)) 
			{
				pDataPagging.ShowEmptyMessage ();;
				return false; 
			}
			 
			pDataPagging.PaggingInfoNoChanges = pPagedValues.PaggingInfo;
			
			return LoadDataList<O> (pDataList, pPagedValues.Results);
		}
		
		
		public static bool LoadDataList<O> (DataList pDataList, IEnumerable <O> pItems) 
		{
			Clear(pDataList);

			pDataList.Visible = true;
			
			if (DataHelper.IsEmpty (pItems))
			{
				return false;
			}			
			
			pDataList.DataSource = pItems;
			pDataList.DataBind ();

			return true;
		}


		public static bool LoadDataList (DataList pDataList, DataTable pdtbTable)
		{
			Clear(pDataList);

			pDataList.Visible = true;
		
			if (DataHelper.IsEmpty (pdtbTable))
			{
				return false;
			}

			pDataList.Visible = true;
			pDataList.DataSource = pdtbTable;
			pDataList.DataBind ();

			return true;
		}
		
		#endregion
	}
}